<?php

namespace App\Imports;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Illuminate\Support\Carbon;
use PhpOffice\PhpSpreadsheet\Shared\Date;
abstract class CommonImport implements ToCollection,WithCalculatedFormulas
{
    public $count = 0;
    public $countSuccess = 0;
    public $countFail = 0;
    public $records = [];
    public $success = true;
    public $msg = '成功！';
    public $params;


    protected $instance;

    protected function getAllCols(){
        return Schema::getColumnListing($this->instance->getTable());
    }

    public function transformDateTime($value, string $format = 'Y-m-d H:i:s'){
        if (!is_numeric($value)) {
            return $value;
        }
        {
            try {
                return Carbon::instance(Date::excelToDateTimeObject($value))->format($format);
            } catch (\ErrorException $e) {
                return Carbon::createFromFormat($format, $value);
            }
        }
    }

}
